<?php
# ***** BEGIN LICENSE BLOCK *****
# This file is part of "myWiWall".
# Copyright (c) 2007-2008 CRP Henri Tudor and contributors.
# All rights reserved.
#
# "myWiWall" is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as published by
# the Free Software Foundation.
# 
# "myWiWall" is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with "myWiWall"; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# ***** END LICENSE BLOCK *****


/**
* @package myWiWall
* @author Laurent Haan <laurent.haan@gmail.com>
*/

$bgcolor = (isset($_GET['bgcolor'])?$_GET['bgcolor']:'');
$fontcolor = (isset($_GET['fontcolor'])?$_GET['fontcolor']:'');
$items = (isset($_GET['items'])?$_GET['items']:'');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
	<title></title>
	<script type="text/javascript" src="../../js/jquery/jquery.js"></script>

	<script type="text/javascript">
	var widgetCache;
	var cloud;
	var maxSize = 250;
	var minSize = 100;
	
	function Tag(label, count)
	{
		this.label = label;
		this.count = count;
	}
	
	function Cloud(tags)
	{
		this.tags = tags;
		
		this.output = function()
		{
			$('#tags').empty();
			
			var max = null, min = null;
			/* get the min/max of the counters */
			for(i = 0; i < this.tags.length; i++)
			{
				if(max == null || this.tags[i].count > max)
					max = this.tags[i].count;
					
				if(min == null || this.tags[i].count < min)
					min = this.tags[i].count;
			}
			
			/* find the range of values */
			var spread = max - min;
			if (spread == 0)
				spread = 1;
			
			var step = (maxSize - minSize)/spread;

			for (i = 0; i < this.tags.length; i++)
			{
				var size = minSize + ((this.tags[i].count - min) * step);
				var a = document.createElement('a');
				a.id = this.tags[i].label;
				a.href = '#';
				a.className = 'tag';
				a.style.fontSize = Math.ceil(size) + '%';
				
				a.appendChild(document.createTextNode(this.tags[i].label));
				a.onclick = function()
				{
					/* empty the widget cache */
					widgetCache = null;
					
					/* request widgets with that specific tag */
					$.getJSON('../../index.php/engine/getWidgetList', {format: 'json', tag: this.id}, function(data){
						widgetCache = data;
						displayResult(0);
					});
				}
				
				document.getElementById('tags').appendChild(a);
				document.getElementById('tags').appendChild(document.createTextNode(' '));
				
				$('#tags').show();
				$('#results').hide();
			}
		}
	}

	function displayResult(start)
	{
		if (start < 0)
			start = 0;
		
		var nbr = <?php echo $_GET['result']; ?>;
		
		$('#results').empty();
		
		var top = document.createElement('div');
		top.id='top';
		
		var back = document.createElement('a');
		back.href='#';
		back.id='backLink';
		back.onclick = function(){
			cloud.output();
		};
		back.appendChild(document.createTextNode('Back to Tags'));
		
		top.appendChild(back);
		$('#results').append(top);
		
		var ul = document.createElement('ul');
		
		for(i=start; i < widgetCache.length && i < start + nbr; i++)
		{
			var li = document.createElement('li');
			li.className = 'widgetList';
			
			var sub = document.createElement('a');
			sub.href='#';
			sub.id = widgetCache[i].id;
			sub.onclick = function()
			{
				parent.addWidget(this.id);
				return false;
			}
			
			sub.target= '_parent';
			sub.className = 'widgetLink';
			sub.appendChild(document.createTextNode(widgetCache[i].name));
			
			li.appendChild(sub);
			li = ul.appendChild(li);
		}
		
		$('#results').append(ul);
		
		var control = document.createElement('div');
		control.id = 'control';
		
		if(start > 0)
		{
			var prev = document.createElement('a');
			prev.className = 'controlItem';
			prev.href= '#';
			prev.onclick = function(){
				displayResult(start - nbr);
			}
			prev.appendChild(document.createTextNode('« Previous'));
			control.appendChild(prev);
		}
		
		control.appendChild(document.createTextNode(' '));
		
		if((start + nbr) < widgetCache.length)
		{
			var next = document.createElement('a');
			next.className = 'controlItem';
			next.href='#';
			next.onclick = function(){
				displayResult(start + nbr);
			}
			next.appendChild(document.createTextNode('Next »'));
			control.appendChild(next);
		}
		
		$('#results').append(control);
		
		/* show the result */
		$('#tags').hide();
		$('#results').show();
	}
	
	function onLoad()
	{
		document.body.style.backgroundColor = '<?php echo $bgcolor; ?>';
		
		$.getJSON('../../index.php/engine/getTags',{'cloud': '<?php echo $items; ?>'}, function(data){
			
			var tags = new Array();
			for(i in data)
			{
				tags.push(new Tag(i, data[i]));
			}

			cloud = new Cloud(tags);
			cloud.output();
		});
	}

	</script>
	<style type="text/css">
	
	#control {
		text-align: center;
	}
	
	#tags {
		font: 12px arial;
	}
	
	li.widgetList {
		list-style: none;
	}
	
	a.widgetLink {
		background: url(../../views/default/img/arrow.gif) no-repeat 0 .2em;
		padding-left: 1.2em;
		color: white;
		text-decoration: none;
	}
	
	#backLink {
		color:white;
	}
	
	a.tag {
		margin: 10px;
		color: <?php echo $fontcolor; ?>;
		text-decoration: none;
	}
	
	a.controlItem {
		color: <?php echo $fontcolor; ?>;
	}
	</style>
</head>
<body>
<div id="tags"></div>
<div id="results" style="display: hidden;"></div>
</body>
</html>
